package com.project.template.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.template.common.PageVO;
import com.project.template.common.Result;
import com.project.template.entity.Posts;
import com.project.template.entity.SecondHandGoods;
import com.project.template.service.BannerService;
import com.project.template.service.SecondHandGoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：lhp
 */
@RestController
@RequestMapping("/secondHandGoods")
public class SecondHandGoodsController {

    @Resource
    private SecondHandGoodsService secondhandgoodsService;

    @ApiOperation(value = "列表",notes = "列表")
    @GetMapping("/list")
    private Result<List<SecondHandGoods>> list(){
        return new Result().success(secondhandgoodsService.list());
    }
    @GetMapping("/mypage")
    public Result<PageVO<Posts>> findMyPage(
            @RequestParam Map<String, Object> query,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        Page<SecondHandGoods> page= secondhandgoodsService.mypage(query,pageNum,pageSize);
        return new Result().success(new PageVO<>(page));
    }
    @GetMapping("/page")
    public Result<PageVO<SecondHandGoods>> findPage(
            @RequestParam Map<String,Object> query,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize
    ){
        Page<SecondHandGoods> page= secondhandgoodsService.page(query,pageNum,pageSize);
        return new Result().success(new PageVO<>(page));
    }

    @PostMapping("/add")
//    @Validated
    public Result add(@RequestBody SecondHandGoods secondHandGoods){
        secondHandGoods.setPublicTime(DateUtil.now());
        secondhandgoodsService.save(secondHandGoods);
        return new Result<>().success();
    }

    @PutMapping("/update")
//    @Validated
    public Result updateById(@RequestBody SecondHandGoods secondHandGoods){
        secondhandgoodsService.updateById(secondHandGoods);
        return new Result<>().success();
    }

    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestBody List<Integer> ids){
        secondhandgoodsService.removeByIds(ids);
        return new Result().success();
    }
    @PostMapping("/delete")
    public Result del(@RequestBody Map<String, Integer> request){
        if (request == null || request.get("id") == null) {
            return new Result().error("参数错误");
        }
        secondhandgoodsService.removeById(request.get("id"));
        return new Result().success();
    }

}
